<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.xnx3.DateUtil"%>
<%@page import="com.xnx3.j2ee.Global"%>
<%@page import="com.xnx3.wangmarket.admin.G"%><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<jsp:include page="../../../../iw/common/head.jsp">
    <jsp:param name="title" value="编辑文件"/>
</jsp:include>

<form id="form" method="post" class="layui-form" style="padding:10px; padding-top:20px; padding-right: 20px;margin-bottom: 10px; margin-top:0px;">
	<input type="hidden" value="${file.id }" name="id">
	<input type="hidden" value="${file.size }" name="size" id="size">
	<input type="hidden" value="${file.suffix }" name="suffix" id="suffix">

	<div class="layui-form-item">
		<label class="layui-form-label" id="label_columnName">文件</label>
		<div class="layui-input-block">
			<input name="url" id="url" type="text" autocomplete="off" placeholder="点击右侧添加" class="layui-input" value="${file.url}" style="padding-right: 120px;">
			<button type="button" class="layui-btn" id="uploadImagesButton" style="float: right;margin-top: -38px;">
				<i class="layui-icon layui-icon-upload"></i>
			</button>
			<a href="#" id="titlePicA" style="float: right;margin-top: -38px;margin-right: 60px;">
				<img id="suffixImg" style="height: 36px;max-width: 57px; padding-top: 1px; display:none;">
			</a><input class="layui-upload-file" type="file" name="fileName">
		</div>
	</div>
		
	
	
   	<div class="layui-form-item">
		<label class="layui-form-label" id="label_columnName">文件名称</label>
		<div class="layui-input-block">
			<input type="text" name="name" id="name" lay-verify="name" autocomplete="off" placeholder="限30个字符以内" class="layui-input" value="${file.name }">
		</div>
	</div>

	<div class="layui-form-item" style="padding-top:15px;">
		<div class="layui-input-block">
			<button class="layui-btn" lay-submit="" lay-filter="demo1">保存修改</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>


<script>
//为当前文件填充后缀名的图片
function fillSuffixImg(suffix){
	if(suffix.length > 0){
	//http://res.weiunity.com/image/filesuffix/png.ico
		document.getElementById("suffixImg").src = 'http://res.weiunity.com/image/filesuffix/'+suffix+'.ico';
		document.getElementById("suffixImg").style.display='';	//让其显示
	}
}
//第一次打开显示的文件后缀名
fillSuffixImg('${file.suffix}');

layui.use('upload', function(){
	var upload = layui.upload;
	//上传图片
	upload.render({
		elem: '#uploadImagesButton' //绑定元素
		,url: '<%=basePath %>plugin/innerDown/uploadFile.do' //上传接口
		,field: 'filename'
		,accept: 'file'
		,size: ${maxFileSizeKB}
		,exts:'${ossFileUploadImageSuffixList }'	//可上传的文件后缀
		,done: function(res){
			//上传完毕回调
			parent.iw.loadClose();
			if(res.result == 1){
				console.log(res.url);
				try{
					document.getElementById("url").value = res.url;
					document.getElementById("size").value = res.size;
					document.getElementById("suffix").value = res.suffix;
					//document.getElementById("titlePicA").href = res.url;
					fillSuffixImg(res.suffix);
					//如果文件名字未填写，那么上传成功后，将原本的文件名字自动赋予文件名这个输入框
					if(document.getElementById("name").value.length == 0){
						document.getElementById("name").value = res.name;
					}
				}catch(err){}
				parent.iw.msgSuccess("上传成功");
			}else{
				parent.iw.msgFailure(res.info);
			}
		}
		,error: function(index, upload){
			//请求异常回调
			parent.iw.loadClose();
			parent.iw.msgFailure();
		}
		,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
			parent.iw.loading('上传中..');
		}
	});
});
</script>
<script>
//自适应弹出层大小
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
parent.layer.iframeAuto(index);

layui.use(['form', 'layedit', 'laydate', 'element'], function(){
  var form = layui.form;

  //自定义验证规则
  form.verify({
    name: function(value){
      if(value.length == 0 || value.length > 50){
        return '请输入50个字以内的文件名称';
      }
    }
  });
  
  //监听提交
  form.on('submit(demo1)', function(data){
  		parent.iw.loading('保存中');
		var d=$("form").serialize();
        $.post("fileSave.do", d, function (obj) { 
        	parent.iw.loadClose();
        	if(obj.result == '1'){
        		parent.parent.iw.msgSuccess("操作成功");
        		parent.location.reload();	//刷新父窗口
        		parent.layer.close(index);
        	}else if(obj.result == '0'){
        		parent.layer.msg(obj.info, {shade: 0.3})
        	}else{
        		parent.layer.msg(result, {shade: 0.3})
        	}
         }, "json");
		
    return false;
  });
  
});


</script>

<jsp:include page="../../../../iw/common/foot.jsp"></jsp:include>